//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Shared/Base/Version.h"

// Helper Mcro //

// build version
#define RESOURCE_VERSION( _ver )		( (ENGINE_VERSION & 0xFFFF0000) | (_ver & 0xFFFF) )

// check header
#define RESFILE_CHECK_HEADER( _file, _type ) \
	{ \
		_shared_resources_::_type const	__header_info; \
		_shared_resources_::_type		__resfile_info; \
		\
		CHECK( _file->Read( __resfile_info ) ); \
		\
		if ( __resfile_info.signature != __header_info.signature ) { \
			RETURN_ERR( "Invalid signature: 0x" << string().FormatI( __resfile_info.signature, 16 ) << "!" ); \
		} \
		if ( __resfile_info.version != __header_info.version ) { \
			RETURN_ERR( "Invalid version: 0x" << string().FormatI( __resfile_info.version, 16 ) << "!" ); \
		} \
	}

// is header
#define RESFILE_IS_HEADER( _file, _type, _result ) \
	{ \
		_shared_resources_::_type const	__header_info; \
		_shared_resources_::_type		__resfile_info; \
		usize const	__pos = _file->Pos(); \
		\
		CHECK( _file->Read( __resfile_info ) ); \
		\
		_result =	( __resfile_info.signature == __header_info.signature ) and \
					( __resfile_info.version == __header_info.version ); \
		\
		_file->SeekSet( __pos ); \
	}

// save header
#define RESFILE_WRITE_HEADER( _file, _type ) \
	{ \
		const _shared_resources_::_type	__hdr_type; \
		\
		CHECK( _file->Write( __hdr_type ) ); \
	}


namespace Engine
{
namespace _shared_resources_
{

	struct ResourceFileInfo
	{
		uint32	signature;
		uint32	version;
		///
		ResourceFileInfo (uint32 sign, uint32 ver) : signature(sign), version(ver) {}
	};


	struct TextureFileInfo : ResourceFileInfo
	{
		static const uint32		def_signature	= CompileTime::TSignature4< 'T', 'E', 'X', ' ' >::value;
		static const uint32		def_version		= RESOURCE_VERSION( 1 );

		TextureFileInfo () : ResourceFileInfo( def_signature, def_version ) {}
	};


	struct ProgramFileInfo : ResourceFileInfo
	{
		static const uint32		def_signature	= CompileTime::TSignature4< 'P', 'R', 'O', 'G' >::value;
		static const uint32		def_version		= RESOURCE_VERSION( 3 );

		ProgramFileInfo () : ResourceFileInfo( def_signature, def_version ) {}
	};


	struct FontFileInfo : ResourceFileInfo
	{
		static const uint32		def_signature	= CompileTime::TSignature4< 'F', 'O', 'N', 'T' >::value;
		static const uint32		def_version		= RESOURCE_VERSION( 1 );

		FontFileInfo () : ResourceFileInfo( def_signature, def_version ) {}
	};


	struct SoundFileInfo : ResourceFileInfo
	{
		static const uint32		def_signature	= CompileTime::TSignature4< 'S', 'N', 'D', ' ' >::value;
		static const uint32		def_version		= RESOURCE_VERSION( 1 );

		SoundFileInfo () : ResourceFileInfo( def_signature, def_version ) {}
	};


	struct UIFrameFileInfo : ResourceFileInfo
	{
		static const uint32		def_signature	= CompileTime::TSignature4< 'U', 'I', 'F', 'R' >::value;
		static const uint32		def_version		= RESOURCE_VERSION( 1 );

		UIFrameFileInfo () : ResourceFileInfo( def_signature, def_version ) {}
	};


	struct UILocaleFileInfo : ResourceFileInfo
	{
		static const uint32		def_signature	= CompileTime::TSignature4< 'U', 'I', 'L', 'L' >::value;
		static const uint32		def_version		= RESOURCE_VERSION( 1 );

		UILocaleFileInfo () : ResourceFileInfo( def_signature, def_version ) {}
	};


	struct UILSettingsFileInfo : ResourceFileInfo
	{
		static const uint32		def_signature	= CompileTime::TSignature4< 'U', 'I', 'L', 'S' >::value;
		static const uint32		def_version		= RESOURCE_VERSION( 1 );

		UILSettingsFileInfo () : ResourceFileInfo( def_signature, def_version ) {}
	};


	struct SpriteFileInfo : ResourceFileInfo
	{
		static const uint32		def_signature	= CompileTime::TSignature4< 'S', 'P', 'R', '2' >::value;
		static const uint32		def_version		= RESOURCE_VERSION( 1 );

		SpriteFileInfo () : ResourceFileInfo( def_signature, def_version ) {}
	};

}	// _shared_resources_
}	// Engine

#undef RESOURCE_VERSION
